<?php
class ElongPayment {

	static $version = "1";
	static $input_charset = "utf8";
	static $cur = "CNY";
	static $md5_key = "e16cdcb13831845eaaaf84613eacc18c";
	var $orders = null;


	var $data;
	function __construct($orders){
		$this->orders = $orders;
		$this->initData();
	}

	static function getPayUrl($out_trade_no, $subject, $body, $total_fee, $pay_mode ){
	}
	function getFormInputStr($arr){
		$ret = '';
		foreach ($arr as $name => $value) {
			$ret .= "<input name='$name' value='$value' type='hidden'/>\n";
		}
		return $ret;
	}


	/**
	 *
	 *
	 * @param unknown_type $transaction_id elong 信用卡支付的该字段最好是每此请求生成一次
	 * @param unknown_type $order_id
	 * @param unknown_type $subject
	 * @param unknown_type $body
	 * @param unknown_type $total_fee
	 */

	//TODO: 这里要删除第一个参数
	function purchase($transaction_id,$order_id, $subject, $body, $total_fee){
		$transaction_id = '';
		$db = Database::getInstance();
		$row = $db->queryOne("select transaction_id from  elongpay_trans where oid = ? and DATE_ADD( createdt, INTERVAL 5 MINUTE)>CURRENT_TIMESTAMP order by transaction_id desc ",$order_id);
		if ($row) {
			$transaction_id = $order_id."_".$row['transaction_id'];
		}else{
			$insert_rows = $db->execute("INSERT INTO  `elongpay_trans` (`oid`) VALUES (?)",$order_id);
			if ($insert_rows == 1) {
				$transaction_id = $db->getInsertId ();
				$transaction_id = $order_id."_".$transaction_id;
			}
		}
		$this->data["transaction_type"] = "purchase";
		$this->data["transaction_id"] = $transaction_id;
		$this->data["order_id"] = $order_id;
		$this->data["amt"] = $total_fee;
		$this->data["product_name"] = $subject;
		$this->data["product_desc"] = $body;
		$this->data["sign"] = $this->getSign();
		return $this->data;
	}

	//'merchant_id,notify_id,notify_time,sign,out_transaction_id,amt,transaction_date,
	//transaction_over_date,operate_status,cur,expand_field,error_code'
	function notify_verify($merchant_id,$notify_id,$notify_time,$sign,$out_transaction_id,$amt,$transaction_date,$transaction_over_date,$operate_status,$cur,$expand_field,$error_code){

		$this->data['merchant_id'] = $merchant_id;
		$this->data['notify_id'] = $notify_id;
		$this->data['notify_time'] = $notify_time;
		$this->data['out_transaction_id'] = $out_transaction_id;
		$this->data['amt'] = $amt;
		$this->data['transaction_date'] = $transaction_date;
		$this->data['transaction_over_date'] = $transaction_over_date;
		$this->data['operate_status'] = $operate_status;
		$this->data['cur'] = $cur;
		$this->data['expand_field'] = $expand_field;
		$this->data['error_code'] = $error_code;
		$this->data["sign"] = $this->getSign();

		return $this->data['sign'] == $sign;
	}

	function getSign() {
		$d = $this->data;
		// MD5(amt& cur& expand_field & input_charset & invoice & merchant_id & next_url & notify_url & order_id & other & product_desc & product_id & product_name & product_num & requestdate & requestip & return_url & transaction_id & transaction_type & version &md5key)
		return md5($d['amt'].$d['cur'].$d['expand_field'].$d['input_charset'].$d['invoice'].$d['merchant_id'].$d['next_url'].$d['notify_url'].$d['order_id'].$d['other'].$d['product_desc'].$d['product_id'].$d['product_name'].$d['product_num'].$d['requestdate'].$d['requestip'].$d['return_url'].$d['transaction_id'].$d['transaction_type'].$d['version'].self::$md5_key);
		;
	}

	function initData(){
		$ep_config = Context::getConfig("elong_payment");
		$this->data = array(
		"input_charset"=>self::$input_charset,
		"cur"=>self::$cur,
		"version"=>self::$version,
		"merchant_id"=>$ep_config["merchant_id"],
		"notify_url"=>$ep_config["notify_url"],
		"return_url"=>$ep_config["return_url"],
		"next_url"=>$ep_config["next_url"],


		"transaction_type"=>"",
		"transaction_id"=>'',
		"order_id"=>'',
		"amt"=>'',
		"product_name"=>'',
		"product_desc"=> '',
		"other"=>"", // 地址
		"requestdate"=>date("Y-m-d H:i:s",time()),

		"sign"=>'',
		"product_id"=>"",
		"product_num"=>"",

		"requestip"=>"", //发起请求的服务器IP
		"invoice"=>"", // 发票抬头
		"expand_field"=>"", //
		);
	}
}
?>
